<?php
namespace app\api\controller;

use app\index\model\VisitorModel;
use app\api\model\SwipeModel;

class Index extends Base {
	public function test() {
		$msgInfo = [
			"sender_id" => 12,
			"sender_name" => "测试",
			"sender_head" => $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'],
			"msg_type" => "aaa"
		];
		send_socket_msg("12","this is test",$msgInfo);
	}
	/**
	 * [app首页轮播图数据]
	 */
	public function getSwipeData() {
		$swipeModel = new SwipeModel();
		$data = $swipeModel::select()->toArray();
		foreach($data as &$v){
			$v['link'] = "###";
			$v['img'] = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].$v['url'];
		}
		return json(['code'=>1,'msg'=>'ok','data'=>$data]);
	}

	/**
	 * [app用户数据]
	 */
	public function getUserInfo() {
		$visitorModel = new VisitorModel();
		$data = $visitorModel::where("id",$this->params['id'])->find();
		if(empty($data)){
			return json(['code'=>0,'msg'=>'该用户不存在','data'=>'']);
		}
		$data['head'] = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].$data['head'];
		return json(['code'=>1,'msg'=>'ok','data'=>$data]);
	}

	/**
	 * [保存用户基本信息]
	 */
	public function saveVisitorBaseInfo() {
		$visitorModel = new VisitorModel();
		$visitorModel::startTrans();
		try {
			$visitorModel::where("id",$this->params['id'])->update($this->params);
			$visitorModel::commit();
			return json(['code'=>1,'msg'=>'保存成功','data'=>""]);
		} catch (\Exception $e) {
			$visitorModel::rollback();
			return json(['code'=>0,'msg'=>'保存失败：'.$e->getMessage(),'data'=>""]);
		}

	}
}
